
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import norsys.netica.Node;


public class association {
	  	  
	  private HashMap<String, associationElement> snp_proxy_association ;
	  
	  private int totalDuplicateAssociation;
	  private int totalAssociation;
	  private boolean printDebug = false;
	  public void association() {
		  if(snp_proxy_association == null){
			snp_proxy_association = new HashMap<String, associationElement>();
		   	
		  }
		  totalDuplicateAssociation = 0;
		  totalAssociation = 0;
		  
		
	  }
	  public void setPrintDebug(boolean debug)
	  {
		  printDebug = debug;
	  }
	  
	  public void setAssociation(snp thisSnp, snp thisProxy, Float rSquared, 
			  String g_trait, String g_trait_odds,
			  String p_trait, String p_trait_odds)
	  {
		  associationElement ae = new associationElement(thisSnp, thisProxy, rSquared);
		  String key = thisSnp.getName() + "-" + thisProxy.getName();
		  String altKey = thisProxy.getName() + "-" + thisSnp.getName();
		  
		  if(snp_proxy_association != null){
			  if(!snp_proxy_association.containsKey(key)) 
			  {
				  if( !snp_proxy_association.containsKey(altKey) )
				  {
					  ae.getGsnp().setTraitOdds(g_trait, new Float(g_trait_odds));
				  	  ae.getPsnp().setTraitOdds(p_trait, new Float(p_trait_odds));
					  snp_proxy_association.put(key, ae);
					  totalAssociation++;
				  }
				  else
				  {
					  //System.out.println("Duplicate association found for: " + key);
					  associationElement elem = snp_proxy_association.get(key);
					  if(elem != null)
					  {
						  elem.getGsnp().setTraitOdds(g_trait, new Float(g_trait_odds));
						  elem.getPsnp().setTraitOdds(p_trait, new Float(p_trait_odds));
					  }
					  totalDuplicateAssociation++;
				  }
			  }
			  else if(!snp_proxy_association.containsKey(altKey)) 
			  {
				  if( !snp_proxy_association.containsKey(key) )
				  {
					  ae.getGsnp().setTraitOdds(g_trait, new Float(g_trait_odds));
				  	  ae.getPsnp().setTraitOdds(p_trait, new Float(p_trait_odds));
					  snp_proxy_association.put(key, ae);
					  totalAssociation++;
				  }
				  else
				  {
					  if(printDebug)
					  {
						  System.out.println("Duplicate association found for: " + key);
					  }
					  associationElement elem = snp_proxy_association.get(key);
					  if(elem != null)
					  {
						  elem.getGsnp().setTraitOdds(g_trait, new Float(g_trait_odds));
					  	  elem.getPsnp().setTraitOdds(p_trait, new Float(p_trait_odds));
					  }
					  
					  totalDuplicateAssociation++;
				  }
			  }
			  
		  }
		  else
		  {
			  ae.getGsnp().setTraitOdds(g_trait, new Float(g_trait_odds));
			  ae.getPsnp().setTraitOdds(p_trait, new Float(p_trait_odds));
			  snp_proxy_association = new HashMap<String, associationElement>();
			  snp_proxy_association.put(key, ae);
			  totalAssociation++;
		  }
		  
	  }
	  
	  public HashMap<String, associationElement> getAssociations(){
		  return snp_proxy_association;
	  }
	  
	  public associationElement getAssociation(String key){
		  return snp_proxy_association.get(key);
	  }
	  
	  
	  public Iterator<Map.Entry<String, associationElement>> getIterator() {
		  	return snp_proxy_association.entrySet().iterator();
	  }
	  
	  public void clear() {
		  if(snp_proxy_association != null){
				  snp_proxy_association.clear();
		  }
	  }
	  
	  public int getDuplicateAssociation() {
		  	return totalDuplicateAssociation;
	  }

	  public int getTotalAssociation() {
		  	return totalAssociation;
	  }

}
